package com.bruce.algorithm.leetcode.linklist;

/**
 * @author luotuan
 * @Url https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/
 * @Description 83. 删除排序链表中的重复元素
 * @create 2020-07-18 17:51
 **/
public class LinkListRemoveRepet {
    public static ListNode deleteDuplicates(ListNode head) {
        ListNode nowHead = null;
        ListNode preNode = nowHead;
        ListNode lastNode = null;
        while (head != null) {
            int value = head.val;
            if (lastNode == null) {
                nowHead = new ListNode(value);
                preNode = nowHead;
            }
            if (lastNode != null && value != lastNode.val) {
                preNode.next = new ListNode(value);
                preNode = preNode.next;

            }
            lastNode = head;
            head = head.next;
        }
        return nowHead;
    }

    /**
     *   1 , 1 ,2 ,3,4,5,5
     * @param head
     * @return
     */
    public static ListNode deleteDuplicates2(ListNode head){
        if(head == null || head.next==null){
            return head;
        }
        head.next = deleteDuplicates(head.next);
        if(head.val == head.next.val){
            return head.next;
        }
        return head;
    }

    public static void main(String[] args) {
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(1);
        ListNode node3 = new ListNode(1);
        node1.next = node2;
        node2.next = node3;
        ListNode result = deleteDuplicates(node1);
        while (result != null) {
            System.out.print(result.val + ",");
            result = result.next;
        }
    }
}
